package com.example.exam.sysRole.service;

import com.example.exam.db.PageInfoBean;
import com.example.exam.sysRole.dao.SysRoleDao;
import com.example.exam.sysRole.model.SysRoleBean;
import com.example.exam.user.dao.UserRoleDao;
import com.example.exam.user.model.UserRoleBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleService {
    @Autowired
    SysRoleDao srd;
    @Autowired
    UserRoleDao userRoleDao;
    /**
     * @Description:
     * @Param: [srb]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */ public Integer insertRole(SysRoleBean srb){
        Integer i=srd.add(srb);
        if (i>0){
            return 1;
        }else{
            return -1;
        }
    }/**
     * @Description:
     * @Param: [id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer deleteRole(Integer id){
        Integer i=srd.removeById(id);
        if (i>0){
            return 1;
        }else{
            return -1;
        }
    }/**
     * @Description:
     * @Param: [id]
     * @return: com.example.exam.sysRole.model.SysRoleBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public SysRoleBean selectOneRole(Integer id){
        SysRoleBean srb=srd.findById(id);
        if(srb==null){
            return null;
        }else{
            return srb;
        }
    }/**
     * @Description:
     * @Param: [srb]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer updateRole(SysRoleBean srb){
        Integer i=srd.updateAllFieldById(srb);
        if (i>0){
            return 1;
        }else{
            return -1;
        }
    }/**
     * @Description:
     * @Param: [pageNo, pageSize, roleName, roleId]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean getRoleList(Integer pageNo, Integer pageSize, String roleName,Integer roleId) {
        PageInfoBean page = new PageInfoBean();
        page.setPerPage(pageSize);
        if(pageNo != null) {
            page.setCurPage(pageNo);
        }
        page = srd.queryRoleList(page,roleName,roleId);
        return page;
    }/**
     * @Description:
     * @Param: [userId]
     * @return: java.util.List<com.example.exam.sysRole.model.SysRoleBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysRoleBean> getRoleList(Integer userId,Integer id){
        List<SysRoleBean> roleList = new ArrayList<SysRoleBean>();
        List<SysRoleBean> roleBeans =srd.getRoleList(userId);
        List<UserRoleBean> userRoleBeans = userRoleDao.findByUserId(id);
        for (SysRoleBean roleBean : roleBeans) {
            for (UserRoleBean userRoleBean : userRoleBeans) {
                if(roleBean.getId()==userRoleBean.getRoleId()) {
                    roleBean.setCreateUser(0);
                    break;
                }else {
                    roleBean.setCreateUser(1);
                }
            }
            roleList.add(roleBean);
        }
        return roleList;
    }
    public List<String> getUserRolesName(int userId) {
        List<SysRoleBean> roleList = srd.queryUserRoles(userId);
        List<String> roleNames = new ArrayList<String>();
        if (roleList != null && roleList.size() > 0) {
            for (int i = 0; i < roleList.size(); i++) {
                SysRoleBean bean = roleList.get(i);
                roleNames.add(bean.getName());
            }
        }
        return roleNames;
    }
}
